package com.app.designpattern.creation.builder;

/**
 * USER: zjw
 * DATA: 2019/4/10 0010
 * TIME: 上午 11:34
 * 建造者模式:把复杂的对像的构建和表示分离出来,可以使不同的构建展示相同的表示
 * 复杂的对像:一类中有很多的属性(成员变量),并且创建实例时,并不需要指定所有的属性,可以选择的
 * 构建:通过builder建造者(静态的内部类,不持有外部类的引用,避免内存泄漏),可以使用链式调用
 * <p>
 * 一个课程的类,里面有name,video,note,qa(问答)等属性,但是创建实例是,可以选择是否指定note或者qa
 *
 */
public class Course {
    private String courseName;
    private String courseVideo;
    private String courseNote;
    private String courseQA;

    public Course(Builder builder) {
        this.courseName = builder.courseName;
        this.courseVideo = builder.courseVideo;
        this.courseNote = builder.courseNote;
        this.courseQA = builder.courseQA;
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseName='" + courseName + '\'' +
                ", courseVideo='" + courseVideo + '\'' +
                ", courseNote='" + courseNote + '\'' +
                ", courseQA='" + courseQA + '\'' +
                '}';
    }

    public static class Builder {
        private String courseName;
        private String courseVideo;
        private String courseNote;
        private String courseQA;

        public Builder builderCourseName(String courseName) {
            this.courseName = courseName;
            return this;
        }

        public Builder builderCourseVideo(String courseVideo) {
            this.courseVideo = courseVideo;
            return this;
        }

        public Builder builderCourseNote(String courseNote) {
            this.courseNote = courseNote;
            return this;
        }

        public Builder builderCourseQA(String courseQA) {
            this.courseQA = courseQA;
            return this;
        }

        public Course builder() {
            return new Course(this);
        }
    }

}
